package com.niodata.dp.task.dao;

import com.niodata.dp.task.entity.PluginCategoryCountItem;
import com.niodata.dp.task.entity.PluginPackage;
import com.niodata.dp.task.entity.PluginPackageView;
import com.niodata.dp.task.entity.PluginSysCategory;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface PluginPackageMapper2 {


  /**
   * 根据插件ID查询插件详细信息.
   *
   * @param pkgId 插件ID
   * @return 对应的插件详细信息
   */
  public PluginPackage getPluginByPkgId(@Param("pkgId") String pkgId);

  @Select("select * from PLUGINS where PKG_NAME=#{name} and CURRENT=1")
  @ResultMap("packageResultMap")
  public PluginPackage getPluginCurrentVersion(@Param("name") String name);

  /**
   * 根据名字和版本获取插件信息.
   *
   * @param name 插件名字
   * @param version 插件版本
   */
  @Select("select * from PLUGINS WHERE PKG_NAME=#{pkgName} and PKG_VERSION=#{pkgVersion}")
  @ResultMap("packageResultMap")
  public PluginPackage getPluginByNameVersion(@Param("pkgName") String name,
      @Param("pkgVersion") String version);

  /**
   * 根据名字和版本获取插件信息.
   *
   * @param name 插件名字
   */
  public List<PluginPackage> getPluginByName(@Param("pkgName") String name);

  /**
   * 根据插件ID删除插件信息.
   **/
  public void deletePluginByPkgId(@Param("pkgId") String pkgId);

  /**
   * 新增插件信息.
   **/
  public void addPlugin(@Param("pkg") PluginPackage pkg);

  public List<PluginPackageView> getUserAuthorizedPlugins(@Param("userId") Long userId,
      @Param("sysCategory") String sysCategory,
      @Param("funcCategory") String funcCategory,
      @Param("nameContains") String nameContains,
      @Param("offset") int offset,
      @Param("count") int count);


  public List<PluginPackageView> getPluginsPublicToHub(
      @Param("sysCategory") String sysCategory,
      @Param("funcCategory") String funcCategory,
      @Param("nameContains") String nameContains,
      @Param("offset") int offset,
      @Param("count") int count);

  @Select("select distinct PKG_TYPE from PLUGINS")
  public List<String> getPluginTypes();

  @Select("select distinct SYS_CATEGORY FROM PLUGINS")
  public List<String> getSysCategories();

  @Select("select distinct FUNCTION_CATEGORY FROM PLUGINS WHERE SYS_CATEGORY=#{sysCategory}")
  public List<String> getFuncCategories(@Param("sysCategory") String sysCategory);

  @Update("update PLUGINS set SYS_CATEGORY=#{sysCategory},FUNCTION_CATEGORY=#{funcCategory}"
      + " where PKG_ID=#{pkgId}")
  public void updateCategory(@Param("pkgId") String pkgId,
      @Param("sysCategory") PluginSysCategory sysCategory,
      @Param("funcCategory") String funcCategory);

  @Update("update PLUGINS SET DESCRIPTION=#{description}"
      + " where PKG_ID=#{pkgId}")
  public void updateDescription(@Param("description") String description,
      @Param("pkgId") String pkgId);

  @Update("update PLUGINS set DESCRIPTION=#{plugin.description},"
      + "FUNCTION_CATEGORY=#{plugin.funcCategory},"
      + "SYS_CATEGORY=#{plugin.sysCategory},"
      + "STATUS=#{plugin.status},"
      + "TAGS=#{plugin.tags},"
      + "PARAMS=#{plugin.params},"
      + "PKG_LANG=#{plugin.pkgLang},"
      + "JOB_TYPE=#{plugin.jobType},"
      + "OUT_PARAMS=#{plugin.outParams},"
      + "SOURCE_TAG=#{plugin.sourceTag},"
      + "SOURCE_REPO=#{plugin.sourceRepo},"
      + "CURRENT=#{plugin.current},"
      + "MIN_PLAT_VERSION=#{plugin.minPlatformVersion},"
      + "SCM_TYPE=#{plugin.scmType},"
      + "MAINTAINERS=#{plugin.maintainers},"
      + "DEPENDENCIES=#{plugin.dependencies},"
      + "CHANGES=#{plugin.changes},"
      + "IS_PUBLIC=#{plugin.publicOn},"
      + "FAIL_RETRY=#{plugin.failRetry},"
      + "UPDATE_TIME=#{plugin.updateTime}"
      + " where PKG_ID=#{plugin.pkgId}")
  public void update(@Param("plugin") PluginPackage plugin);


  @Update("update PLUGINS SET TASK_COUNT=(SELECT COUNT(1) FROM JOB_INFO "
      + "where PKG_ID=#{pkgId}) where PKG_ID=#{pkgId}")
  public void updatePluginUseCount(@Param("pkgId") String pkgId);

  @Select("SELECT count(distinct a.PKG_NAME) AS count,a.SYS_CATEGORY as sysCategory,"
      + "a.FUNCTION_CATEGORY as funcCategory  FROM ("
      + " SELECT p.* FROM PLUGINS p,USER_GROUP ug where (p.OWNER_ID=ug.GROUP_ID "
      + "and p.OWNER_TYPE='GROUP'"
      + " and ug.USER_ID=#{userId} and ug.ROLE in ('MASTER','OWNER','MEMBER'))"
      + " union all"
      + " SELECT p.* FROM PLUGINS p where p.IS_PUBLIC = 1"
      + " union all"
      + " SELECT p.* FROM PLUGINS p where p.OWNER_TYPE = 'USER' and p.OWNER_ID =#{userId}"
      + " ) AS a WHERE a.CURRENT=1"
      + " group by a.SYS_CATEGORY,a.FUNCTION_CATEGORY")
  public List<PluginCategoryCountItem> getPluginCategoryCount(@Param("userId") long userId);


  @Select("SELECT count(distinct a.PKG_NAME) AS count,a.SYS_CATEGORY as sysCategory,"
      + "a.FUNCTION_CATEGORY as funcCategory  FROM ("
      + " SELECT p.* FROM PLUGINS p where p.IS_PUBLIC = 1 and PUBLIC_TO_HUB=1"
      + " ) AS a WHERE a.CURRENT=1"
      + " group by a.SYS_CATEGORY,a.FUNCTION_CATEGORY")
  public List<PluginCategoryCountItem> getPublicToHubPluginCategoryCount();


  @Update("update PLUGINS set DOWN_TIMES=#{downTimes} where PKG_NAME=#{name} and "
      + "PKG_VERSION=#{version}")
  public void updateDownTimes(@Param("name") String name,
      @Param("version") String version,
      @Param("downTimes") int downTimes);

  @Select("select * from PLUGINS ORDER BY PKG_NAME,PKG_VERSION desc")
  @ResultMap("packageResultMap")
  public List<PluginPackage> getAllPlugins();

}
